<?php
namespace plugins\goods\controller;

use cmf\controller\PluginAdminBaseController;
use plugins\goods\model\GoodsAuditModel;
use plugins\store\Common;
use think\Db;

/**
 *
 */
class GoodsAuditController extends PluginAdminBaseController
{
    private $AM;
    private $pag     = "10";
    private $pageNum = "10";
    protected function _initialize()
    {
        $this->AM = new GoodsAuditModel;
    }
    /**
     * 商品审核列表
     */
    public function index()
    {
        $param = $this->request->param();
        $pagesize = isset( $param['pagesize'] ) ? $param [ 'pagesize' ] :10;
    
        if( !empty($param['status']) && ($param['status'] >0 )){
            $where = " `audit_status` =" . $param['status']; 
        }elseif ( !empty($param['status']) && ($param['status'] <= 0)) { 
            $where = 1;
        }else{  
            $where = " `audit_status` = 1 ";
        }
        if (!empty($param['title'])) {
            if ($param['type'] == 1) {
                $where .= " and `goods_name` like '%" . $param['title'] . "%'";
            }

            if ($param['type'] == 2) {
                $where .= " and `id` =" . $param['title'];
            }
        }
        if(!empty($param['start_time'])){
            $where .= " and upward_time >= ".strtotime($param['start_time']);
        }
        if(!empty($param['end_time'])){
            $where .= " and upward_time <= ".strtotime($param['end_time']);
        }    
        
        //权限
        $is_admin = Common::getAllowAccess();
        if (true !== $is_admin) {
            $where .= " and store_id = '".$is_admin."'";
        }
  
        //检查日志是否需要新增申请
        $data = db('goods')->where('audit_status', 1)->select()->each(function($value){
            $audit = db('goods_audit')->where('goodsid', $value['id'])->find();
            if (!$audit) {
                db('goods_audit')->insert(['goodsid' => $value['id'], 'status' => 1, 'time' => $value['add_time']]);
            }
        });
             
        $info = db('goods')->where($where)->order('add_time', 'desc')->paginates($pagesize);
        foreach( $info as $key => $value ){// ,'time'=>['>=',$value['upward_time']]
            $find = db('goods_audit')->where([ 'goodsid' => $value['id'], 'status' => $value['audit_status'] ])->order('time desc')->find();
            if( !$find )
            { 
                unset($info[$key]);
                continue;
            }else{
                $value['time'] = $find['time'];
                $value['reason'] = $find['reason'];
            }
            
            $info[$key] = $value;
        }
            
        $info->appends($param);
        $this->assign('page', $info->render()); //单独提取分页出来
        $this->assign('data', $info);

        $this->assign('param', $param);
        return $this->fetch('goods_audit/index');
    }
    /**
     * 审核通过
     */
    public function audit()
    {
        $param = $this->request->post();

        $id    = $param['goodsid'];
        //修改状态
        $data = db('goods')->where('id', $id)->setField('audit_status', 2);
        if (!$data) {
            $this->error('操作失败');
        }
        //添加日志
        $info = $this->AM->journal('goodsid', $id, 2);
        if (!$info) {
            $this->error('审核日志添加失败');
        }

        $this->success('操作成功');
    }
    /**
     * 审核驳回
     */
    public function reject()
    {
        $param  = $this->request->param();
        $id     = $param['goodsid'];
        $reason = $param['reason'];
        //修改状态
        $data = db('goods')->where('id', $id)->setField('audit_status', 3);
        if (!$data) {
            $this->error('操作失败');
        }
        //添加日志
        $info = $this->AM->journal('goodsid', $id, 3, $reason);
        if (!$info) {
            $this->error('审核日志添加失败');
        }
        $this->success('操作成功');
    }
    /**
     * 批量通过
     */
    public function batchAudit()
    {
        $param = $this->request->post();

        if (empty($param['id'])) {
            $this->error('请选择待操作的数据~');
        }

        $id    = $param['id'];
        foreach ($id as $key => $value) {
            //修改状态
            $data = db('goods')->where('id', $value)->setField('audit_status', 2);
            if (!$data) {
                $this->error('出现失败批量审核中断');
            }

            //添加日志
            $info = $this->AM->journal('goodsid', $value, 2);
            if (!$info) {
                $this->error('审核日志添加失败，批量审核中断');
            }
        }

        return $this->success('操作成功');
    }
    /**
     * 批量驳回
     * store_status 状态为 3
     */
    public function batchReject()
    {
        $param  = $this->request->param();

        if (empty($param['id'])) {
            return $this->error('请选择待操作的数据~');
        }
        $id     = $param['id'];
        $reason = $param['reason'];
        foreach ($id as $key => $value) {
            //修改状态
            $data = db('goods')->where('id', $value)->setField('audit_status', 3);
            if (!$data) {
                $this->error('出现失败批量驳回中断');
            }
            //添加日志
            $info = $this->AM->journal('goodsid', $value, 3, $reason);
            if (!$info) {
                $this->error('审核日志添加失败，批量驳回中断');
            }
        }
        $this->success('驳回成功');
    }
    /**
     * 查看日志
     */
    public function logPage()
    {
        $param = $this->request->param();
        $id    = $param['gid'];
        $data  = db('goods_audit')->where('goodsid', $id)->order('time desc')->select();
        $this->assign('data', $data);
        return $this->fetch('goods_audit/logpage');
    }

    /**
     * 审核设置
     */
    public function auditSet()
    {
        $data = $this->request->param();
        if ($this->request->isGet()) {

            $config = getModuleConfig( 'goods' , "config/conf", "audit_set.json" );
            $config = json_decode( $config , true );

            $this->assign('config', $config);
            return $this->fetch('goods_audit/auditSet');
        }

        $da = [
            'audit_open' => $data['audit_open'],
        ];

        saveModuleConfigData( 'goods', "config/conf", "audit_set.json", $da );

        return $this->success('操作成功');
    }


    // /**
    //  * 查看提交内容详细
    //  */
    // public function detailsPag()
    // {
    //     $param = $this->request->param();
    //     $id = $param['gid'];
    //     $info = db( 'goods' )->where( 'id' , $id )->find();
    //     //如果有安装商品分类Cats
    //     $GoodsCats = './plugins/goods/config/install/goods_classify.log';
    //     if( file_exists( $GoodsCats ) )
    //     {
    //         $cats = new \plugins\goods\controller\GoodsCatsController();
    //         $info['cats'] = $cats->pidname($info['catid']);
    //     }
    //     //如果有安装商品规格
    //     $GoodsSpec = './plugins/goods/config/install/goods_spec.log';
    //     if( file_exists( $GoodsSpec ) )
    //     {
    //         $spec = new \plugins\goods\controller\GoodsSpecController();
    //         // $info['spec'] = $spec->pidname($info['catid']);
    //     }
    //     $this->assign( 'data' , $info );
    //     return $this->fetch( 'audit_goods/viewPage' );
    // }
}
